package com.ljxt.dt.service.storemanagementservice.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.dao.storemanagementdao.AddReceptDao;
import com.ljxt.dt.dao.storemanagementdao.impl.AddReceptDaoImpl;
import com.ljxt.dt.domain.*;
import com.ljxt.dt.service.storemanagementservice.AddReceptService;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.util.ServiceUtil;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.AddFindFoodsVO;
import com.ljxt.dt.vo.storemanagement.GoodsDate;
import com.ljxt.dt.vo.storemanagement.GoodsInfoVO;
import com.ljxt.dt.vo.storemanagement.JsonBen;

import java.beans.BeanInfo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-12 17:26
 * @copyright 老九学堂
 */

public class AddReceptServiceImpl implements AddReceptService {
    private AddReceptDao admin = new AddReceptDaoImpl();
    @Override
    public List<ReceptType> selectAddType() {
        return admin.selectAddType();
    }

    @Override
    public List<SupplierInfo> selectSupplier() {
        return admin.selectSupplier();
    }

    @Override
    public PageVO selectFoods(AddFindFoodsVO addFindFoodsVO,int pageIndex,int pageSize) {
        List<GoodsInfo> goodsInfos =  admin.findFoods(addFindFoodsVO,pageIndex,pageSize);
        int total = admin.selectTotal(addFindFoodsVO);

        return new PageVO(goodsInfos,pageIndex,pageSize,total);
    }

    @Override
    public List<GoodsInfoVO> selectGoodsInfo(String[] strs) {
        //得到商品信息
        List<GoodsInfo> goodsInfos = admin.selectGoodsInfo(strs);

        List<GoodsInfoVO> goodsInfoVOS = new ArrayList<>();

        //循环查到每个商品对应的属性
      for (int i = 0 ; i<goodsInfos.size();i++){
          //得到商品属性
            List<String> types =admin.selectGoodsType(goodsInfos.get(i).getId());
            //创建一个VO
          GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
          //设置值
          goodsInfoVO = (GoodsInfoVO) ServiceUtil.setObj(goodsInfos.get(i),goodsInfoVO);
          //创建一个map 设vo里面的值
          Map<String,List<AttrInfo>> goodsTypesMap = new HashMap<>();
          Map<String,String> attrId = new HashMap<>();
            //用来储存当前属性的id
          //循环得到该商品对应的属性的值
          for (String str: types) {

              //得到属性的名字
              CategoryInfo name = admin.selectGoodsInfoName(str);
              //得到所有的属性值对象
              List<AttrInfo> goodsInfoTypes = admin.selectGoodsInfoType(str);

                //设置值到map里面
                goodsTypesMap.put(name.getCategoryName(),goodsInfoTypes);
                //设置属性的id

                attrId.put(name.getCategoryName(),name.getId().toString());
          }
            //将设置好的值的map放到vo中
          goodsInfoVO.setGoodsTypes(goodsTypesMap);
            goodsInfoVO.setAttrId(attrId);
          //将vo加到集合中
          goodsInfoVOS.add(goodsInfoVO);
      }
        return goodsInfoVOS ;
    }

    @Override
    public int insertRecept(JsonBen jsonBen) {

      //创建一个入库单
        ReceptInfo receptInfo = new ReceptInfo();
        DateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
        receptInfo.setReceptNo("RK"+format.format(new Date()));
        receptInfo.setCreatedate(new Date());
        receptInfo.setRemakers(jsonBen.getRemark());
        receptInfo.setTypeId(jsonBen.getTypeId());
        receptInfo.setStateId(1);

        int rows = admin.insertRepect(receptInfo);
        //循环将入库单关联的入库商品添加
        List<GoodsDate> goodsDates = jsonBen.getGoodsInfoDate();
        for (GoodsDate goodsDate : goodsDates) {
            ReceptDetail receptDetail = new ReceptDetail();
            receptDetail.setAttrId(goodsDate.getAttrId());
            receptDetail.setReceptAmount(goodsDate.getReceptAmount());
            receptDetail.setReceptNo(receptInfo.getReceptNo());
            receptDetail.setReceptAmount(goodsDate.getReceptAmount());
            receptDetail.setGoodsId(goodsDate.getGoodsId());

            receptDetail.setCompleteAmount(goodsDate.getReceptAmount());
            receptDetail.setBrokenAmount(0);
            String name =  DaoUtil.querySelectString("select goods_name from goods_info where id = ?",goodsDate.getGoodsId());
            receptDetail.setGoodsName(name);
            int row =  admin.insertRepectDetail(receptDetail);

        }

        return rows;
    }
}
